﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using ZedGraph;
using System.IO;

namespace Common.Modules
{
    /**/
    /// <summary>
    /// 显示统计图形类型
    /// </summary>
    public enum AnalyticsType
    {
        Line,   //折线图
        Bar,    //柱状图
        Pie     //饼图
    }

    public class ZedGraphPainter
    {
        #region Private Attribute
        /**/
        /// <summary>
        /// 默认颜色种类
        /// </summary>
        private List<Color> defaultColors = new List<Color>();
        /**/
        /// <summary>
        /// 统计的个数
        /// </summary>
        private int Count;

        ZedGraph.Web.ZedGraphWeb webObject;

        System.Drawing.Graphics g;

        ZedGraph.MasterPane pane;

        #endregion

        #region Public Property
        /**/
        /// <summary>
        /// 统计图的名称
        /// </summary>
        public string Title;
        /**/
        /// <summary>
        /// 横轴的名称（饼图不需要）
        /// </summary>
        public string XAxisTitle;
        /**/
        /// <summary>
        /// 纵轴的名称（饼图不需要）
        /// </summary>
        public string YAxisTitle;
        /**/
        /// <summary>
        /// 显示的曲线类型：Line,Bar,Pie
        /// </summary>
        public AnalyticsType Type;
        /**/
        /// <summary>
        /// 折线图和柱状图的数据源
        /// </summary>
        public List<PointPairList> DataSource = new List<PointPairList>();
        /**/
        /// <summary>
        /// 饼图的数据源
        /// </summary>
        public List<double> ScaleData = new List<double>();
        /**/
        /// <summary>
        /// 各段数据的颜色
        /// </summary>
        public List<Color> Colors = new List<Color>();
        /**/
        /// <summary>
        /// 各段数据的名称
        /// </summary>
        public List<string> NameList = new List<string>();
        /**/
        /// <summary>
        /// 用于柱状图，每个圆柱体表示的含义
        /// </summary>
        public List<string> LabelList = new List<string>();
        #endregion

        public ZedGraphPainter()
        {
            webObject = new ZedGraph.Web.ZedGraphWeb();
            pane = new MasterPane();
            pane.PaneList.Add(new GraphPane());
        }

        private void InitDefaultColors()
        {
            defaultColors.Add(Color.Red);
            defaultColors.Add(Color.Green);
            defaultColors.Add(Color.Blue);
            defaultColors.Add(Color.Yellow);
            defaultColors.Add(Color.YellowGreen);
            defaultColors.Add(Color.Brown);
            defaultColors.Add(Color.Aqua);
            defaultColors.Add(Color.Cyan);
            defaultColors.Add(Color.DarkSeaGreen);
            defaultColors.Add(Color.Indigo);
        }
        /**/
        /// <summary>
        /// 如果属性为空则初始化属性数据
        /// </summary>
        private void InitProperty()
        {
            InitDefaultColors();
            if (string.IsNullOrEmpty(Title))
            {
                Title = "未命名统计图";
            }
            if (string.IsNullOrEmpty(XAxisTitle))
            {
                XAxisTitle = "横轴";
            }
            if (string.IsNullOrEmpty(YAxisTitle))
            {
                YAxisTitle = "纵轴";
            }
            if (Type == AnalyticsType.Pie)
            {
                Count = ScaleData.Count;
            }
            else
            {
                Count = DataSource.Count;
            }
            if (Colors.Count == 0 || Colors.Count != Count)
            {
                Random r = new Random();
                int tempIndex = 0;
                List<int> tempIndexList = new List<int>();
                for (int i = 0; i < Count; i++)
                {
                    tempIndex = r.Next(defaultColors.Count);
                    if (tempIndexList.Contains(tempIndex))
                    {
                        i--;
                    }
                    else
                    {
                        tempIndexList.Add(tempIndex);
                        Colors.Add(defaultColors[tempIndex]);
                    }
                }
            }
            if (NameList.Count == 0)
            {
                if (Type == AnalyticsType.Bar)
                {
                    for (int i = 0; i < DataSource[0].Count; i++)
                    {
                        NameList.Add("第" + i.ToString() + "组");
                    }
                }
                else
                {
                    for (int i = 0; i < Count; i++)
                    {
                        NameList.Add("第" + i.ToString() + "组");
                    }
                }
            }
            if (LabelList.Count == 0)
            {
                for (int i = 0; i < Count; i++)
                {
                    LabelList.Add("含义" + i.ToString());
                }
            }
        }
        /**/
        /// <summary>
        /// 画图51-a-s-px
        /// </summary>
        /// <param name="webObject"></param>
        /// <param name="g"></param>
        /// <param name="pane"></param>
        public void Draw()
        {
            InitProperty();
            var myPane = pane.PaneList[0];
            myPane.Title.Text = Title;
            myPane.XAxis.Title.Text = XAxisTitle;
            myPane.YAxis.Title.Text = YAxisTitle;
            switch (Type)
            {
                case AnalyticsType.Line:
                    DrawLine(myPane);
                    break;
                case AnalyticsType.Bar:
                    DrawBar(myPane);
                    break;
                case AnalyticsType.Pie:
                    DrawPie(myPane);
                    break;
                default:
                    break;
            }
            //pane.AxisChange(g);
            pane.AxisChange();
        }

        #region Draw
        /**/
        /// <summary>
        /// 画折线图
        /// </summary>
        /// <param name="graphPane"></param>
        private void DrawLine(GraphPane graphPane)
        {
            for (int i = 0; i < Count; i++)
            {
                graphPane.AddCurve(NameList[i], DataSource[i], Colors[i], SymbolType.None);
            }
        }
        /**/
        /// <summary>
        /// 画柱状图
        /// </summary>
        /// <param name="graphPane"></param>
        private void DrawBar(GraphPane graphPane)
        {
            for (int i = 0; i < Count; i++)
            {
                graphPane.AddBar(LabelList[i], DataSource[i], Colors[i]).Bar.Fill = new Fill(Colors[i], Color.White, Colors[i]);
            }
            graphPane.XAxis.MajorTic.IsBetweenLabels = true;
            string[] labels = NameList.ToArray();
            graphPane.XAxis.Scale.TextLabels = labels;
            graphPane.XAxis.Type = AxisType.Text;
            graphPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
            graphPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
        }
        /**/
        /// <summary>
        /// 画饼图
        /// </summary>
        /// <param name="graphPane"></param>
        private void DrawPie(GraphPane graphPane)
        {
            graphPane.Fill = new Fill(Color.White, Color.Silver, 45.0f);

            graphPane.Legend.Position = LegendPos.Float;
            graphPane.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction, AlignH.Right, AlignV.Top);
            graphPane.Legend.FontSpec.Size = 20f;
            graphPane.Legend.IsHStack = false;

            for (int i = 0; i < Count; i++)
            {
                graphPane.AddPieSlice(ScaleData[i], Colors[i], Color.White, 45f, 0, NameList[i]);
            }
        }
        /**/
        /// <summary>
        /// 如果系统出错，显示错误信息
        /// </summary>
        /// <param name="graphPane"></param>
        /// <param name="message"></param>
        private void DrawMessage(GraphPane graphPane, string message)
        {
            TextObj text = new TextObj(message, 200, 200);
            text.Text = message;
            graphPane.GraphObjList.Add(text);

        }

        #endregion

        public Image GenerateImage()
        {
            return pane.GetImage();
        }
    }

}